#/bin/bash

dollName=
if [ $# -gt 0 ]; then
    dollName=$1
fi

baseurl=http://10.98.0.172:1304/download/doll
lstfile=$(curl $baseurl/lasversion)
echo "wget $baseurl/$lstfile -O doll_bin.tar.gz"
wget $baseurl/$lstfile -O doll_bin.tar.gz

tmpdir=/tmp/doll_bin
mkdir -p $tmpdir
rm -fr $tmpdir/*
if [ -f doll_bin.tar.gz ]; then
    tar xzf doll_bin.tar.gz -C $tmpdir
fi

nodedir=TARS_PATH/tarsnode
if [ -d $tmpdir/doll/config ]; then
    mkdir -p $nodedir/config 
    rm -fr $nodedir/config/*
    cp -fr $tmpdir/doll/config/* $nodedir/config
fi

dirname=$(ls $nodedir/data/ | grep srv | head -1)
if [ ! -z "$dirname" ]; then
    dollName=${dirname%.*}
fi

DOLLS="accountsrv battlesrv configsrv dbsrv gamesrv gmproxysrv managesrv msdksrv ranksrv routersrv sandboxsrv smssrv tbattlesrv uidsrv wordsfiltersrv"
for var in ${DOLLS};
do
    if [ -d $nodedir/data/$dollName.$var ] && [ -d $nodedir/data/$dollName.$var/bin ] && [ -d $tmpdir/doll/$var/bin ]; then
        echo "copy $nodedir/data/$dollName.$var"
        cp -fr $tmpdir/doll/$var/bin/* $nodedir/data/$dollName.$var/bin/
        chmod +x $nodedir/data/$dollName.$var/bin/*.sh
        chmod +x $nodedir/data/$dollName.$var/bin/$var
    fi
done

rm -fr $tmpdir/* doll_bin.tar.gz